/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc. X
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2017/01/17 by CS $
*/
$(function(){
	//选择支付类型
	$(document).on("click", "[f='but_select_paytype']", function(){
		$val = $(this).attr("data-val");
		$("[f='but_select_paytype']").removeClass("current");
		$(this).addClass("current");
		$("#paytype").val($val);
	});

	//选择在线支付方式
	$(document).on("click", "[f='but_select_onlinepay']", function(){
		$_sdkid = $(this).attr("data-sdkid");
		$_sdkname = $(this).attr("data-sdkname");
		$("[f='but_select_onlinepay']").removeClass("current");
		$(this).addClass("current");

		$("#sdkid").val($_sdkid);
		$("#sdkname").val($_sdkname);
	});

	//提交支付
	$(document).on("click", "[f='but_confirm_pay']", function(){
		$paytype = $("#paytype").val();
		$paynum = $("#paynum").val();
		$sdkid = $("#sdkid").val();
		$sdkname = $("#sdkname").val();
		$bankid = $("#bankid").val();

		if ($paytype == "onlinepay") {
			if ($sdkid.length == 0) {
				ToastShow("请选择支付方式");
				return false;
			}
		}
		
		if ($paytype == "moneypay") { //余额支付
			$.ajax({
				type: "POST",
				url: _ROOT_PATH + "index.php?c=pay",
				cache: false,
				data: {a:"paymoney", paynum:$paynum, r:get_rndnum(8)},
				dataType: "json",
				success: function($data) {
					$json = eval($data);
					$response = $json.response;
					$result = $json.result;
					if ($response == "1") { //成功
						window.location.href = _ROOT_PATH+"index.php?c=pay&a=show&paynum="+$paynum;
					}
					else if ($response == "2") { //重复
						window.location.href = _ROOT_PATH+"index.php?c=pay&a=show&paynum="+$paynum;
					}
					else {
						if ($result.length > 0) {
							ToastShow($result);
						}
						else {
							ToastShow("余额支付失败，请使用在线支付！");
						}
					}
				},
				error: function() {
					ToastShow("系统繁忙，请稍后再试");
				}
			});
		}

		else { //在线支付方式
			$.ajax({
				type: "POST",
				url: _ROOT_PATH + "index.php?c=pay",
				cache: false,
				data: {a:"savepaysdk", paynum:$paynum, sdkid:$sdkid, sdkname:$sdkname, bankid:$bankid, r:get_rndnum(8)},
				dataType: "json",
				success: function($data) {
					$json = eval($data);
					$response = $json.response;
					$result = $json.result;
					if ($response == "1") { //成功
						window.location.href = _ROOT_PATH+"index.php?c=pay&a=submit&paynum="+$paynum;
					}
					else {
						if ($result.length > 0) {
							ToastShow($result);
						}
						else {
							ToastShow("支付失败，请检查！");
						}
					}
				},
				error: function() {
					ToastShow("系统繁忙，请稍后再试");
				}
			});
		
		}

	});

});